package response

import (
	"carSpider/data"
	"encoding/json"
	"fmt"
	goSpider "github.com/Flyits/goSpider/src"
	"github.com/antchfx/htmlquery"
	"golang.org/x/net/html"
	"strings"
)

func Config(response goSpider.Response, spider *goSpider.Spider) {
	var config []data.Config
	subConfig, nextNode := getSubConfig(htmlquery.FindOne(response.HtmlNode, "//div[@class='wrapper']/div"))
	config = append(config, subConfig)
	for {
		if nextNode == nil {
			break
		}
		subConfig, nextNode = getSubConfig(nextNode)
		config = append(config, subConfig)
	}

	configStr, _ := json.Marshal(config)
	fmt.Println(string(configStr))
	version := data.Version{ID: response.Attach.(map[string]int)["versionId"]}
	data.Db.Model(&version).Update("param_config", configStr)
}

func Image(response goSpider.Response, spider *goSpider.Spider) {
	fmt.Println("图片爬取：", response.Url)
	for _, titleNode := range htmlquery.Find(response.HtmlNode, "//div[@class='tt-first tt-first-no-bd']") {
		if htmlquery.FindOne(titleNode, ".//h3/text()").Data == "官方" {
			moreImageNode := htmlquery.FindOne(titleNode, ".//a")
			if moreImageNode != nil {
				moreImageUrl := htmlquery.SelectAttr(moreImageNode, "href")

				spider.AddJob(goSpider.UrlItem{
					Url:         "http://photo.m.yiche.com" + moreImageUrl,
					HandlerFunc: moreImage,
					Attach:      response.Attach,
				})
			} else {
				var images []string
				for _, image := range htmlquery.Find(response.HtmlNode, "//div[@class='pic-select-car'][last()]//ul//li") {
					images = append(images, strings.Replace(htmlquery.SelectAttr(htmlquery.FindOne(image, ".//img"), "src"), "_4", "_8", -1))
				}

				imagesJson, _ := json.Marshal(images)
				fmt.Println(string(imagesJson), response.Attach.(map[string]int)["versionId"])
				version := data.Version{ID: response.Attach.(map[string]int)["versionId"]}
				data.Db.Model(&version).Update("images", imagesJson)
			}
		}
	}
}

func moreImage(response goSpider.Response, spider *goSpider.Spider) {
	var images []string
	for _, image := range htmlquery.Find(response.HtmlNode, "//ul[@class='ul-list']//li") {
		images = append(images, strings.Replace(htmlquery.SelectAttr(htmlquery.FindOne(image, ".//img"), "src"), "_4", "_8", -1))
	}
	imagesJson, _ := json.Marshal(images)
	fmt.Println(string(imagesJson), response.Attach.(map[string]int)["versionId"])
	version := data.Version{ID: response.Attach.(map[string]int)["versionId"]}
	data.Db.Model(&version).Update("images", imagesJson)
}

func getSubConfig(configNode *html.Node) (data.Config, *html.Node) {
	var subConfig data.Config
	subConfig.Name = htmlquery.SelectAttr(configNode, "data-flag")
	for _, ValueNode := range htmlquery.Find(configNode, "/table/tbody/tr") {
		name := htmlquery.SelectAttr(ValueNode, "data-name")
		valueClass := htmlquery.SelectAttr(htmlquery.FindOne(ValueNode, "/td"), "class")
		// 若为颜色
		if valueClass == "color-box" {
			var value []string
			for _, colorNode := range htmlquery.Find(ValueNode, "//i") {
				value = append(value, strings.TrimLeft(htmlquery.SelectAttr(colorNode, "style"), "background-color:"))
			}
			subConfig.SubConfig = append(subConfig.SubConfig, data.SubConfig{
				Name:  name,
				Value: value,
			})
		} else {
			subConfig.SubConfig = append(subConfig.SubConfig, data.SubConfig{
				Name:  name,
				Value: goSpider.Trim(htmlquery.FindOne(ValueNode, "//span[1]/text()").Data, nil),
			})
		}
	}
	return subConfig, htmlquery.FindOne(configNode, "/div[2]")

}
